package com.ganmingshan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ganmingshan.admin.dao.SignDao;
import com.ganmingshan.admin.entity.SignEntity;
import com.ganmingshan.admin.entity.ToSignEntity;
import com.ganmingshan.admin.service.SignService;
import com.ganmingshan.admin.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

@Transactional
@Service("signService")
public class SignServiceImpl extends ServiceImpl<SignDao, SignEntity> implements SignService {
    @Autowired
    private SignDao signDao;

    @Override
    public List<SignEntity> allList(LocalTime laterTime) {
        List<SignEntity> signEntities = this.baseMapper.selectList(null);
        List<SignEntity> endEntities = checkIsLater(signEntities,laterTime);
        return endEntities;
    }

    @Override
    public  HashMap<String,Long> deptLater() {
        List<HashMap<String, Long>> list = signDao.deptLater();
        HashMap<String,Long> map = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            for (HashMap<String,Long> hashMap : list) {
                String key = null;
                Integer value = null;
                for (Map.Entry<String,Long> entry : hashMap.entrySet()) {
                    if ("key".equals(entry.getKey())) {
                        key = String.valueOf(entry.getValue());
                    } else if ("value".equals(entry.getKey())) {
                        value = (entry.getValue()).intValue();
                    }
                }
                map.put(key, Long.valueOf(value));
            }
        }
        return map;
    }

    @Override
    public void uploadExcel(List<ToSignEntity> entities) {
        for (ToSignEntity entity : entities) {
            SignEntity signEntity = new SignEntity();
            signEntity.setStatus(entity.getStatus());
            signEntity.setCheckin(entity.getCheckin());
            signEntity.setDepartment(entity.getDepartment());
            signEntity.setName(entity.getName());
            signDao.insert(signEntity);
        }
    }

    @Override
    public void export(HttpServletResponse response) {
        List<Object> head = Arrays.asList("name", "department", "checkin", "status");
        ArrayList<List<Object>> lists = new ArrayList<>();
        LambdaQueryWrapper<SignEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SignEntity::getStatus,1);
        List<SignEntity> entities = this.baseMapper.selectList(wrapper);
        lists.add(head);
        for (SignEntity entity : entities) {
            ArrayList<Object> list = new ArrayList<>();
            list.add(entity.getName());
            list.add(entity.getDepartment());
            list.add(entity.getCheckin());
            list.add(entity.getStatus());
            lists.add(list);
        }
        ExcelUtils.export(response,"迟到表",lists);
    }

    @Override
    public List<SignEntity> findLateList() {
        List<SignEntity> lateList = signDao.findLateList();
        return lateList;
    }

    private List<SignEntity> checkIsLater(List<SignEntity> list,LocalTime laterTime) {
        List<SignEntity> entities = list.stream().map(signEntity -> {
            LocalDateTime checkin = signEntity.getCheckin();
            LocalTime signTime = checkin.toLocalTime();
            if (signTime.isAfter(laterTime)) {
                signEntity.setStatus(1);
                signDao.update(signEntity);
            }
            else {
                signEntity.setStatus(0);
                signDao.update(signEntity);
            }
            return signEntity;
        }).collect(Collectors.toList());
        return entities;
    }
}